package gxprod.androidai.fetch;

import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import android.util.Log;

/**
  *Fetches and parses information from Wolfram|Alpha
  *@author Austin Bolstridge
*/
public class WolframAlpha {
	//Required to do any queries to Wolfram|Alpha
	public static final String APPID = "T6K92A-78E9Y47XKA";
	public static final String SEARCH_URL = "http://api.wolframalpha.com/v2/query?input=";
	
	private String wolframAlphaInfo;
	private Document dom;
	public org.w3c.dom.Element[] List; 
	
	public Document process(String query) {
		String cleanQuery = query.replace("+", "%2B");
		cleanQuery = cleanQuery.replace(" ", "+");
		cleanQuery = cleanQuery.replace(",", "%2C");
		
		
		try {
			wolframAlphaInfo = Utilities.readUrl(new URL(SEARCH_URL + query + "&appid=" + APPID));
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace(); 
		}
		Document doc = Utilities.documentFromString(wolframAlphaInfo, true);
		return doc;
		
	}
	@SuppressWarnings("unused")
	private void parseXmlFile(String xmlFile){
		//get the factory
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

		try {

			//Using factory get an instance of document builder
			DocumentBuilder db = dbf.newDocumentBuilder();

			//parse using builder to get DOM representation of the XML file
			dom = db.parse(xmlFile);

		}catch(Exception ioe) {
			Log.d("", "The world is going to end!!!!");
		}
	}
	@SuppressWarnings("unused")
	private void parseDocument(){
		//get the root element
		org.w3c.dom.Element docEle = dom.getDocumentElement();

		//get a nodelist of elements by the tag "plaintext"
		NodeList nl = docEle.getElementsByTagName("plaintext");
		if(nl != null && nl.getLength() > 0) {
			for(int i = 0 ; i < nl.getLength();i++) {
				org.w3c.dom.Element el = (org.w3c.dom.Element)nl.item(i);
				//TODO Add sorting and storing of Elements
			}
		}
	}
	/**
	 * I take an employee element and read the values in, create
	 * an Employee object and return it
	 */
	@SuppressWarnings("unused")
	private String[][] getEmployee(org.w3c.dom.Element empEl) {

		//for each <employee> element get text or int values of
		//name ,id, age and name
		String name = getTextValue(empEl,"Name");
		int id = getIntValue(empEl,"Id");
		int age = getIntValue(empEl,"Age");

		String type = empEl.getAttribute("type");

		//Create a new Employee with the value read from the xml nodes
		String[][] e = new String[6][];
		

		return e;
	}


	/**
	 * I take a xml element and the tag name, look for the tag and get
	 * the text content
	 * i.e for <employee><name>John</name></employee> xml snippet if
	 * the Element points to employee node and tagName is 'name' I will return John
	 */
	private String getTextValue(org.w3c.dom.Element ele, String tagName) {
		String textVal = null;
		NodeList nl = ele.getElementsByTagName(tagName);
		if(nl != null && nl.getLength() > 0) {
			org.w3c.dom.Element el = (org.w3c.dom.Element)nl.item(0);
			textVal = el.getFirstChild().getNodeValue();
		}

		return textVal;
	}


	/**
	 * Calls getTextValue and returns a int value
	 */
	private int getIntValue(org.w3c.dom.Element ele, String tagName) {
		//in production application you would catch the exception
		return Integer.parseInt(getTextValue(ele,tagName));
	}
}
